package com.newlink.building.userinfo.mvp

import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.model.bean.UpdateAvatarBean
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AccountInformationContract
import com.newlink.building.userinfo.mvp.model.Module_UserInfo_AccountInformationModel
import okhttp3.MultipartBody

/**
 * @author wangyang
 * @date 2022/9/16
 * @description 我的——账户信息页面的Presenter
 */
class Module_UserInfo_AccountInformationPresenter :
    BasePresenter<Module_UserInfo_AccountInformationContract.Model, Module_UserInfo_AccountInformationContract.View>(),
    Module_UserInfo_AccountInformationContract.Presenter {

    override fun createModel(): Module_UserInfo_AccountInformationContract.Model? {
        return Module_UserInfo_AccountInformationModel()
    }

    override fun getUserInfo(token: String, userId: Long) {
        mModel?.getUserInfoNew(token, userId)?.ss(mModel, mView) { infoData ->
            logDebug<Module_UserInfo_AccountInformationPresenter>("getUserInfo response ->  ${infoData?.data}")
            if (infoData?.data == null) {
                mView?.getUserInfoFail()
            } else {
                mView?.getUserInfoNewSuccess(infoData.data)
            }
        }
    }

    override fun setUserInfo(token: String, userId: String, nickname: String) {
        mModel?.setUserNickName(token, userId, nickname)?.ss(mModel, mView) { infoData ->
            logDebug<Module_UserInfo_AccountInformationPresenter>("[jake]....setUserInfo response ->  ${infoData?.data}")
            if (infoData?.code == 200) {
                mView?.setUserInfoSuccess()
            } else {
                mView?.setUserInfoFail()
            }
        }
    }

    override fun syncUserInfo(token: String, userInfoData: UpdateAvatarBean) {
        mModel?.syncUserInfo(token, userInfoData)?.ss(mModel, mView) { infoData ->
            logDebug<Module_UserInfo_AccountInformationPresenter>("setUserInfo response ->  ${infoData?.data}")
            if (infoData?.code == 200) {
                mView?.setUserInfoSuccess()
            } else {
                mView?.setUserInfoFail()
            }
        }
    }


    override fun uploadAvatar2(token: String, body: MultipartBody.Part) {
        mModel?.uploadAvatar2(token, body)?.ss(mModel, mView) { infoData ->
            logDebug<Module_UserInfo_AccountInformationPresenter>("uploadAvatar2 response ->  ${infoData?.data}")
            if (infoData?.data == null) {
                mView?.uploadAvatarFail()
            } else {
                val avatarResponse = infoData.data
                mView?.uploadAvatarSuccess(avatarResponse)
            }
        }
    }

    override fun deleteAccount(token: String, userId: String, masterFlag: Boolean) {
        mModel?.deleteAccount(token, userId, masterFlag)?.ss(mModel, mView) {
            logDebug<Module_UserInfo_AccountInformationPresenter>("deleteAccount response ->  ${it?.data}")
            if (it?.code == 200) {
                mView?.deleteAccountSuccess()
            } else {
                mView?.deleteAccountFail()
            }
        }
    }
}